package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.wanho.dto.LoginUserDTO;
import net.wanho.entity.User;
import net.wanho.mapper.UserMapper;
import net.wanho.service.UserService;
import net.wanho.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * Author：汤小洋
 * Date：2025-09-19 10:30
 * Description：<描述>
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public UserVo login(LoginUserDTO loginUserDTO) {
        // 条件构造器，用来封装查询条件
        // QueryWrapper<User> wrapper = new QueryWrapper<>();
        // wrapper.eq("username", loginUserDTO.getUsername());
        // wrapper.eq("password", loginUserDTO.getPassword());

        // wrapper.like()
        // wrapper.likeLeft()
        // wrapper.likeRight()

        // wrapper.eq(!ObjectUtils.isEmpty(loginUserDTO.getUsername()), "username", loginUserDTO.getUsername());
        // wrapper.orderByDesc()
        // wrapper.orderByAsc()

        // 使用Lambda语法
        // LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        // wrapper.eq(User::getUsername, loginUserDTO.getUsername()); // 可以避免写错字段名
        // wrapper.eq(User::getPassword, loginUserDTO.getPassword());


        // User user = getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, loginUserDTO.getUsername()));
        User user = getOne(new QueryWrapper<User>().eq("username", loginUserDTO.getUsername()));

        /**
         * 判断是否存在
         */

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);

        return userVo;
    }

    @Override
    public Page<User> findByPage(int pageNum, int pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);

        this.page(page);

        return page;
    }
}
